package com.egame.manage.biz.web.controller.pu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.EnumType.NetworkType;
import cn.egame.interfaces.fl.FileInfo;
import cn.egame.interfaces.fl.FileUsedType;
import cn.egame.interfaces.fl.FileUtils;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.gc.GameTagLinkInfo;
import cn.egame.interfaces.pu.AppParameter;

import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.server.pu.ParameterServiceImpl;
import com.egame.manage.biz.server.pu.StoreSortServiceImpl;
import com.egame.manage.biz.utils.ConstVar;
import com.egame.manage.biz.utils.ConstantVar;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/store/necessary")
public class NecessaryController extends BaseController {

	public NecessaryController(){		
	}
	public NecessaryController(HttpServletRequest req,
			HttpServletResponse resp) {
		super(req, resp);
	}
	
	@RequestMapping(value = "/sort/list", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView listNecessaryPosition(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int sortSelect = WebUtils.getInt(req, "sortSelect", 0);
		ModelAndView mv = new ModelAndView("/new/op/store/necessaryList");
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, sortSelect);
		List<Integer> recommendGids = ManageClient.getInstance().listGameIdByGameTagId(appId,loginUserId,tagid);
		List<GameInfo> recommendtmp = ManageClient.getInstance().listGameInfoByIds(appId, loginUserId, recommendGids);
		List<GameInfo> recommendList = new ArrayList<GameInfo>();
		//对推荐游戏进行排序
		if(recommendtmp != null && recommendtmp.size()>0){
			for(int gid:recommendGids){
				for(GameInfo tmpBean:recommendtmp){
					if(tmpBean != null && tmpBean.getGId() == gid){
						recommendList.add(tmpBean);
					}
				}
			}
		}		
		//过滤推荐游戏
		String recommendName = WebUtils.getString(req, "recommendname","");			
		if(!Utils.stringIsNullOrEmpty(recommendName)){	
			int rid = Utils.toInt(recommendName, 0);
			String rName = rid>0?"":recommendName.trim();
			if(rid>0){
				int i=0;
				while(i<recommendList.size()){
					if(recommendList.get(i).getGId() != rid){
						recommendList.remove(i);
					}else{
						i++;
					}
				}
			}else if(rName.length()>0){
				int i=0;
				while(i<recommendList.size()){
					if(recommendList.get(i).getGameName() != null 
							&& recommendList.get(i).getGameName().indexOf(rName)<0){
						recommendList.remove(i);
					}else{
						i++;
					}
				}
			}			
		}	
		
		String gameName = WebUtils.getString(req, "gname","");
		List<Integer> gids = new ArrayList<Integer>();
		List<GameInfo> gameList = new ArrayList<GameInfo>();		
		if(!Utils.stringIsNullOrEmpty(gameName)){
			int gid = Utils.toInt(gameName, 0);
			String name = gid>0?"":gameName.trim();			
			gids = GameServiceImpl.getInstance().listGIdByName(appId, loginUserId, gid, name, GameStatus.business.value());
			gameList = ManageClient.getInstance().listGameInfoByIds(appId, loginUserId, gids);					
		}	
		//去除推荐游戏	
		gameList.removeAll(recommendList);
		
		mv.addObject("recommendList", recommendList);
		mv.addObject("gameList", gameList);
		mv.addObject("gameName", gameName);
		mv.addObject("recommendName", recommendName);
		//获取应用分类
		List<Integer> appSortIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstantVar.STORE_INSTALLED_NECESSARY);
		List<AppParameter> appSortList = new ArrayList<AppParameter>();
		if((appSortIds != null)&&(appSortIds.size()>0)){
			for(Integer appIds:appSortIds){
				appSortList.add(ManageClient.getInstance().getAppParameterById(appId, loginUserId, appIds)) ;
			}
		}
		mv.addObject("appSortList", appSortList);
		mv.addObject("sortId", sortSelect);
		return mv;			
	}
	
	//新增跳转
	@RequestMapping(value = "/sort/set/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showNewAdvertisement(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		return new ModelAndView("/new/op/store/addNeceSort");
	}	
	//新增提交
	@RequestMapping(value = "/sort/new", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView addAppSort(HttpServletRequest req, HttpServletResponse resp, AppParameter bean) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		bean.setDepth(3);
		bean.setEnable(true);
		bean.setRemark("");
		bean.setOperatorId((int)loginUserId);
		//应用分类父节点
		bean.setParentId(ConstantVar.STORE_INSTALLED_NECESSARY);
		//TODO
		int maxSortNo = ParameterServiceImpl.getInstance().getMaxSortNo(ConstantVar.STORE_INSTALLED_NECESSARY);
		bean.setSort(1+maxSortNo);
		//彩蛋商城Type
		bean.setType(ManageClient.getInstance().getAppParameterById(appId, loginUserId, ConstantVar.STORE_INSTALLED_NECESSARY).getType());
		ManageClient.getInstance().setAppParameter(appId, loginUserId, bean);
		return listNecessaryPosition(req,resp);
	}
	//删除
	@RequestMapping(value = "/sort/delete", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView deleteAppSort(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		String gIds = WebUtils.getString(req, "gid","");
		if(!Utils.stringIsNullOrEmpty(gIds)){
			String[] ids= gIds.split(",");
			for(String id:ids){
				StoreSortServiceImpl.getInstance().deleteSort(appId, loginUserId, Integer.valueOf(id));
			}
		}	
		return listNecessaryPosition(req,resp);
	}
	//编辑跳转
	@RequestMapping(value = "/sort/edit/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView editAppSort(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int id = WebUtils.getInt(req,"id",0);	
		AppParameter bean = ManageClient.getInstance().getAppParameterById(appId, loginUserId, id);
		ModelAndView mv = new ModelAndView("/new/op/store/editNeceSort"); 
		mv.addObject("bean", bean);
		return mv;
	}
	
	//编辑提交
	@RequestMapping(value = "/sort/set/edit", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView setHXAdvertisement(int id,int sort,AppParameter bean,HttpServletRequest req, HttpServletResponse resp)throws Exception{

		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		bean.setId(id);
		bean.setSort(sort);
//				bean.setDepth(4);
//				bean.setEnable(true);
//				bean.setRemark("");
//				bean.setParentId(ConstantVar.STORE_INSTALLED_NECESSARY);
//				bean.setType(ManageClient.getInstance().getAppParameterById(appId, loginUserId, ConstantVar.STORE_INSTALLED_NECESSARY).getType());
		ManageClient.getInstance().setAppParameter(appId, loginUserId, bean);
		
		return listNecessaryPosition(req,resp);
	}
	
	@RequestMapping(value = "/sort/upper", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView upperSortNo(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int sortId = WebUtils.getInt(req,"id",0);
		StoreSortServiceImpl.getInstance().upper(appId, loginUserId, sortId);	
		return listNecessaryPosition(req,resp);
	}	
	//下移
	@RequestMapping(value = "/sort/lower", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView lowerSortNo(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int sortId = WebUtils.getInt(req,"id",0);
		StoreSortServiceImpl.getInstance().lower(appId, loginUserId, sortId);
		return listNecessaryPosition(req,resp);
	}
	
	@RequestMapping(value = "/recommend/sort/add", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView addRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = listGid(req,resp);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, select);
		for(int gid:gids){
			GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, gid, tagid, 0, -1);
		}		
		return listNecessaryPosition(req, resp);
	}

	@RequestMapping(value = "/recommend/sort/delete", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView deleteRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = listGid(req,resp);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId,select);
		for(int gid:gids){
			GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, gid, tagid);
		}		
		return listNecessaryPosition(req, resp);
	}

	@RequestMapping(value = "/recommend/sort/upper", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView upperRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId,select);
		GameTagServiceImpl.getInstance().upper(appId, loginUserId, gid, tagid);
		return listNecessaryPosition(req, resp);
	}

	@RequestMapping(value = "/recommend/sort/lower", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView lowerRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId,select);
		GameTagServiceImpl.getInstance().lower(appId, loginUserId, gid, tagid);
		return listNecessaryPosition(req, resp);
	}

	@RequestMapping(value = "/recommend/sort/top", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView topRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId,select);
		GameTagServiceImpl.getInstance().top(appId, loginUserId, gid, tagid);
		return listNecessaryPosition(req, resp);
	}

	@RequestMapping(value = "/recommend/sort/down", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView downRecommendGame(int select,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId,select);
		GameTagServiceImpl.getInstance().down(appId, loginUserId, gid, tagid);
		return listNecessaryPosition(req, resp);
	}
	
	public List<Integer> listGid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		List<Integer> gids = new ArrayList<Integer>();
		String gidString = WebUtils.getString(req, "gid", "");
		if(!Utils.stringIsNullOrEmpty(gidString)){
			String[] gidArray = gidString.split(",");
			if(gidArray != null && gidArray.length>0){
				for(String gid : gidArray){
					if(Utils.toInt(gid, 0)>0){
						gids.add(Utils.toInt(gid, 0));
					}
				}
			}
		}
		return gids;
	}
}
